package com.animee.day11.slice;

import com.animee.day11.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;

/**分析猜数游戏：
 * 1.进入页面，就生成了随机数
 * 2.点击核对数字按钮，可以比较输入的数字和生成的大小
 * 3.点击查看数字，就可以在绿色框框内显示数字
 * 4.点击重玩一次，就重新生成随机数，然后将输入款和显示款清空
 * */
public class GuessNumAbilitySlice extends AbilitySlice {
    Text randomTv;
    TextField inputTf;
    Button cbBtn,seeBtn,resetBtn;
    int randomNum = 0;   //进入生成的随机数
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_abilitiy_guess);
        initView();

        randomNum = getRandomNum();
    }

    private int getRandomNum() {
        //int num = 最小值+int(Math.random()*(最大值-最小值+1))
        int num = 1000+(int)(Math.random()*(9999-1000+1));
        showToast("生成数字："+num);
        return num;
    }

    private void initView() {
        randomTv = findComponentById(ResourceTable.Id_guess_tv_random);
        inputTf = findComponentById(ResourceTable.Id_guess_tf);
        cbBtn = findComponentById(ResourceTable.Id_guess_btn_check);
        seeBtn = findComponentById(ResourceTable.Id_guess_btn_look);
        resetBtn = findComponentById(ResourceTable.Id_guess_btn_reset);
        MyClickListener listener = new MyClickListener();
        cbBtn.setClickedListener(listener);
        seeBtn.setClickedListener(listener);
        resetBtn.setClickedListener(listener);
    }
    class MyClickListener implements Component.ClickedListener{
        @Override
        public void onClick(Component component) {
            switch (component.getId()) {
                case ResourceTable.Id_guess_btn_check:
                    checkNum();
                    break;
                case ResourceTable.Id_guess_btn_look:
                    randomTv.setText(randomNum+"");
                    break;
                case ResourceTable.Id_guess_btn_reset:
                    randomNum=getRandomNum();
                    randomTv.setText("");
                    inputTf.setText("");
                    break;
            }
        }
    }
    //判断数字的方法
    private void checkNum() {
        String text = inputTf.getText();
        if (text==null || text.isEmpty()){
            showToast("输入内容不能为空~~");
            return;
        }
        int checkNum = 0;
        try {
             checkNum = Integer.parseInt(text);
        }catch (Exception e){
            showToast("输入内容必须为四位数字~~");
            inputTf.setText("");
            return;
        }
        if (checkNum == randomNum){
            showToast("恭喜您，您太厉害了，您猜对了！！");
            randomTv.setText(randomNum+"");
        }
        if (checkNum<randomNum){
            showToast("您猜的数字偏小~");
        }
        if (checkNum>randomNum){
            showToast("您猜的数字偏大~");
        }
    }
    //显示对话框，持续1秒，自动消失，只是起到提示作用
    private void showToast(String s) {
        new ToastDialog(this)
                .setText(s)
                .show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
